﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Initializes a new instance of IMG_Histogram class.
    /// </summary>
    public partial class IMG_Histogram : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// Initializes a new instance of the IMG_Histogram class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_Histogram(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(img_original);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// open button clicked 
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgOriginal);

                    this.chartHistogram.Update();
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ComboBox_RGBHistogram control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ComboBox_RGBHistogram_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.chartHistogram.Series["Red"].Points.Clear();
                this.chartHistogram.Series["Green"].Points.Clear();
                this.chartHistogram.Series["Blue"].Points.Clear();
                this.chartHistogram.Series["Gray"].Points.Clear();
                IMG_Processor.IMG_HistogramOp.IMG_CalculateHistogramValues(this.imgOriginal);
                if (this.comboBoxRGBHistogram.SelectedIndex == 1)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chartHistogram.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 2)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chartHistogram.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 3)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chartHistogram.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 4)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chartHistogram.Series["Gray"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGrayScale[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chartHistogram.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                        this.chartHistogram.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                        this.chartHistogram.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! Choose an image");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_Equalize control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_match_Click(object sender, EventArgs e)
        {
            try
            {
                Entities.IMG_Image img_matched = null;
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    img_matched = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                }

                this.chrtHistogramEqualization.Series["Red"].Points.Clear();
                this.chrtHistogramEqualization.Series["Green"].Points.Clear();
                this.chrtHistogramEqualization.Series["Blue"].Points.Clear();
                this.imgEdited = IMG_Processor.IMG_HistogramOp.IMG_HistogramMatching(this.imgOriginal, img_matched);
                this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                if (this.comboBoxRGBHistogram.SelectedIndex == 1)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 2)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 3)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 4)
                {
                    this.btnEqualize.Visible = false;
                }
                else
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                        this.chrtHistogramEqualization.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                        this.chrtHistogramEqualization.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! Can't match");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_match control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_Equalize_Click(object sender, EventArgs e)
        {
            try
            {
                this.chrtHistogramEqualization.Series["Red"].Points.Clear();
                this.chrtHistogramEqualization.Series["Green"].Points.Clear();
                this.chrtHistogramEqualization.Series["Blue"].Points.Clear();
                this.imgEdited = IMG_Processor.IMG_HistogramOp.IMG_HistogramEqualization(this.imgOriginal);
                this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
                if (this.comboBoxRGBHistogram.SelectedIndex == 1)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 2)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 3)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
                else if (this.comboBoxRGBHistogram.SelectedIndex == 4)
                {
                    this.btnEqualize.Visible = false;
                }
                else
                {
                    for (int i = 0; i < 256; i++)
                    {
                        this.chrtHistogramEqualization.Series["Red"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgRedValues[i]);
                        this.chrtHistogramEqualization.Series["Green"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgGreenValues[i]);
                        this.chrtHistogramEqualization.Series["Blue"].Points.AddXY(i, IMG_Processor.IMG_HistogramOp.ImgBlueValues[i]);
                    }
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! Can't Equalize");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_Histogram control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_Histogram_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }
    }
}
